//package com.qijian.plug.capacity.cost;
//
//import cn.hutool.core.collection.CollectionUtil;
//import com.qijian.finance.domain.Cost;
//import com.qijian.finance.domain.enums.CostTypeEnum;
//import com.qijian.maindata.domain.LineProcess;
//import com.qijian.maindata.domain.ProcessOrder;
//import com.qijian.maindata.domain.ProcessOrderUsers;
//import com.qijian.maindata.domain.Project;
//import com.qijian.maindata.domain.vo.ProcessOperationCheckVo;
//import com.qijian.maindata.domain.vo.ProcessOrderUsersVo;
//import com.qijian.maindata.mapper.ProcessOrderMapper;
//import com.qijian.maindata.service.ILineProcessService;
//import com.qijian.maindata.service.IProcessOrderService;
//import com.qijian.maindata.service.IProcessOrderUsersService;
//import com.qijian.maindata.service.IProjectService;
//import lombok.RequiredArgsConstructor;
//import org.springframework.stereotype.Component;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
//@Component
//@RequiredArgsConstructor
//
//public class ProcessOrderCheckCost extends CostPlugWrapper<ProcessOrder.CostCheck> {
//
//    private final ProcessOrderMapper processOrderMapper;
//    private final IProcessOrderService processOrderService;
//    private final IProcessOrderUsersService processOrderUsersService;
//
//    private final ILineProcessService lineProcessService;
//    private final IProjectService projectService;
//
//
//    @Override
//    @SuppressWarnings("unchecked")
//    protected void operation(List<Object> args) {
//        Set<Long> ids = (Set<Long>) args.get(0);
//
//        List<ProcessOrder> orders = processOrderService.listByIds(processOrderUsersService.listByIds(ids)
//                .stream().map(ProcessOrderUsers::getWorkId).collect(Collectors.toSet()));
//
//        //工序分类
//        Map<Long, String> processType = lineProcessService.listByIds(orders.stream().map(ProcessOrder::getProcessId).collect(Collectors.toSet()))
//                .stream().collect(Collectors.toMap(LineProcess::getProcessId, LineProcess::getProcessTypeName));
//        //获取开工令机台号
//        Map<Long, String> projectMap = projectService.listByIds(orders.stream().map(ProcessOrder::getProjectId).collect(Collectors.toSet())).stream()
//                .collect(Collectors.toMap(Project::getProjectId, a -> String.format("%s(%s#)", a.getProjectNo(), a.getMachineNo())));
//
//
//        //历史top成本数据
//        Map<String, Cost> topCostMap = super.costService.getTopHistoryDataMap(new HashSet<>(projectMap.values()));
//        //历史父类成本数据
//        Map<String, Cost> parentCostMap = super.costService.getHighHistoryDataMap(new HashSet<>(projectMap.values()), CostTypeEnum.PROCESS_ORDER_USERS);
//
//        //历史子类成本数据
//        //使用拼合参数，可以更精确，只查出需要用到的参数，否则可能存在无用数据
//        //组合查询历史参数
//        Set<String[]> childOld = orders.stream().map(o -> new String[]{projectMap.get(o.getProjectId()), processType.get(o.getProcessId())}).collect(Collectors.toSet());
//        Map<String, Cost> childCostMap = costService.getChildHistoryDataMap(childOld);
//
//        orders.forEach(o -> {
//            String element = projectMap.get(o.getProjectId());
//            Cost top = getTopCost(topCostMap, element);
//            Cost high = getHighCost(parentCostMap, element, CostTypeEnum.PROCESS_ORDER_USERS, top);
//            Cost child = getChildCost(childCostMap, element, processType.get(o.getProcessId()), high);
//
//            child.setActualCost(child.getActualCost().add(o.getWorkCost()));
//            high.setActualCost(high.getActualCost().add(o.getWorkCost()));
//            top.setActualCost(top.getActualCost().add(o.getWorkCost()));
//        });
//        //合并所有数据集
//        List<Cost> saveOrUpdateList = new ArrayList<>
//                (topCostMap.size() + parentCostMap.size() + childCostMap.size());
//        saveOrUpdateList.addAll(topCostMap.values());
//        saveOrUpdateList.addAll(parentCostMap.values());
//        saveOrUpdateList.addAll(childCostMap.values());
//
//        super.saveOrUpdateBatch(saveOrUpdateList);
//    }
//
//    @Override
//    protected boolean condition(List<Object> args) {
//        ProcessOperationCheckVo entity = (ProcessOperationCheckVo) args.get(0);
//
//        Set<Long> ids = entity.getUsers().stream().map(ProcessOrderUsersVo::getWuId).collect(Collectors.toSet());
//        List<ProcessOrder> orderList = processOrderMapper.checkWork(ids);
//        if (CollectionUtil.isNotEmpty(orderList)) {
//            //直接从这里执行实际方法
//            this.operation(Collections.singletonList(ids));
//        }
//        return true;
//    }
//
//    @Override
//    public void doSomething(List<Object> args) {
//        this.condition(args);
//    }
//}
